package com.cskaoyan.service.admin.category;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketCategoryExample;
import com.cskaoyan.bean.bo.admin.category.CategoryDeleteBo;
import com.cskaoyan.bean.vo.admin.category.CategoryL1;
import com.cskaoyan.bean.vo.admin.category.CategoryListVo;
import com.cskaoyan.bean.vo.admin.category.CategoryListVo2;
import com.cskaoyan.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/07/15:02
 * @Description:商品类目
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    /**
    * @Author: 宾文杰 @Description: 获取商品全部信息 @Date: 2022/9/7
    */
    @Override
    public CategoryListVo categoryList() {
        CategoryListVo categoryListVo = new CategoryListVo();
        //pid为0的是类别
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        //获取类别
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        ArrayList<CategoryListVo2> categoryListVo2s = new ArrayList<>();
        if (marketCategories!=null) {
            //类别数组
            for (MarketCategory marketCategory : marketCategories) {
                CategoryListVo2 categoryListVo2 = new CategoryListVo2();
                categoryListVo2.setDesc(marketCategory.getDesc());
                categoryListVo2.setIconUrl(marketCategory.getIconUrl());
                categoryListVo2.setId(marketCategory.getId());
                categoryListVo2.setKeywords(marketCategory.getKeywords());
                categoryListVo2.setName(marketCategory.getName());
                categoryListVo2.setPicUrl(marketCategory.getPicUrl());
                categoryListVo2.setLevel(marketCategory.getLevel());
                //类目的id为商品的pid
                Integer pid = marketCategory.getId();
                MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
                MarketCategoryExample.Criteria criteria1 = marketCategoryExample1.createCriteria();
                criteria1.andPidEqualTo(pid);
                //找二级目录
                List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(marketCategoryExample1);
                //有子商品才添加
                if (marketCategories1!=null) {
                    categoryListVo2.setChildren(marketCategories1.toArray());
                }
                categoryListVo2s.add(categoryListVo2);
            }
            categoryListVo.setLimit(categoryListVo2s.size());
            if (categoryListVo2s.size() != 0) {
                categoryListVo.setList(categoryListVo2s.toArray());
            }
            categoryListVo.setTotal(categoryListVo2s.size());
        }
            categoryListVo.setPage(1);
            categoryListVo.setPages(1);
        return categoryListVo;
    }

    /**
    * @Author: 宾文杰 @Description: 创建商品类目 @Date: 2022/9/7
    */
    @Override
    public MarketCategory categoryCreate(MarketCategory marketCategory) {
        //生成当前时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(new Date());
        try {
            Date addTime = dateFormat.parse(format);
            marketCategory.setAddTime(addTime);
            marketCategory.setUpdateTime(addTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketCategoryMapper.insert(marketCategory);
        Integer id = marketCategory.getId();
        MarketCategory category = marketCategoryMapper.selectByPrimaryKey(id);
        return category;
    }

    /**
    * @Author: 宾文杰 @Description: 商品类目信息 @Date: 2022/9/7
    */
    @Override
    public CategoryListVo categoryL1() {
        CategoryListVo categoryListVo = new CategoryListVo();
        //pid为0的是类别
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        //获取类别
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        //类别数组
        ArrayList<CategoryL1> categoryL1s = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            CategoryL1 categoryL1 = new CategoryL1();
            categoryL1.setLabel(marketCategory.getName());
            categoryL1.setValue(marketCategory.getId());
            categoryL1s.add(categoryL1);
        }
        categoryListVo.setTotal(categoryL1s.size());
        categoryListVo.setPages(1);
        categoryListVo.setLimit(categoryL1s.size());
        categoryListVo.setList(categoryL1s.toArray());
        categoryListVo.setPage(1);
        return categoryListVo;
    }

    /**
    * @Author: 宾文杰 @Description: 编辑类目信息 @Date: 2022/9/7
    */
    @Override
    public void categoryUpdate(MarketCategory marketCategory) {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andIdEqualTo(marketCategory.getId());
        marketCategoryMapper.updateByExampleSelective(marketCategory, marketCategoryExample);
    }

    /**
    * @Author: 宾文杰 @Description: 删除类目信息 @Date: 2022/9/7
    */
    @Override
    public void categoryDelete(CategoryDeleteBo categoryDeleteBo) {
        Integer id = categoryDeleteBo.getId();
        MarketCategory[] children = categoryDeleteBo.getChildren();
        marketCategoryMapper.deleteByPrimaryKey(id);
        //有子类才删除
        if (children==null||children.length!=0){
            MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
            criteria.andPidEqualTo(id);
            marketCategoryMapper.deleteByExample(marketCategoryExample);
        }
    }


}
